package faceb.server.dao.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import faceb.server.dao.NewsDao;
import faceb.server.model.News;
import faceb.server.util.MyBatisUtil;
import faceb.server.util.ServerFinal;

@Repository("newsDao")
public class NewsDaoImpl implements NewsDao {
	private SqlSessionFactory factory = MyBatisUtil.getsqlSessionFactory();

	public int insertNews(News news) {
		SqlSession session = factory.openSession();
		int result = session.insert("NewsMapper.insertNews", news);
		session.commit();
		session.close();
		return result;
	}

	public Map<String,Object> getNewsByNewsId(String newsid) {
		SqlSession session = factory.openSession();
		Map<String,Object> map = session.selectOne("NewsMapper.getNewsByNewsId", newsid);
		map.put("n_time", new Date((Long)map.get("n_time")).toLocaleString());
		map.put("image", ServerFinal.SERVER_URL+"userlogo/users/"+map.get("u_id"));
		/*Map<String,Object> map = new HashMap<String, Object>();
		map.put("n_id", news.getN_id());
		map.put("n_topic", news.getN_topic());
		map.put("n_title", news.getN_title());
		map.put("n_u_id", news.getN_u_id());
		map.put("n_time", new Date(news.getN_time()).toLocaleString());
		map.put("n_content", news.getN_content());
		map.put("n_viewcount", news.getN_viewcount());
		map.put("n_like", news.getN_like());*/
		session.close();
		return map;
	}

	public List<News> getNewsByUserId(String userid) {
		SqlSession session = factory.openSession();
		List<News> newses = session.selectList("NewsMapper.getNewsByUserId",
				userid);
		session.close();
		return newses;
	}

	public List<News> getNewsBeforeTime(long time, int count) {
		SqlSession session  =factory.openSession();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("time", time);
		params.put("count", count);
		List<News> newses = session.selectList("NewsMapper.getNewsBeforeTime",params);
		session.close();
		return newses;
	}

	public List<News> getNewsAfterTime(long time, int count) {
		SqlSession session  =factory.openSession();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("time", time);
		params.put("count", count);
		List<News> newses = session.selectList("NewsMapper.getNewsAfterTime",params);
		session.close();
		return newses;
	}
	
	public int deleteNewsById(String newsid) {
		SqlSession session  =factory.openSession();
		int result1 = session.delete("CommentMapper.deleteCommentsByNewsId", newsid);
		int result2 = session.delete("NewsPicMapper.deleteNewsPicsByNewsId", newsid);
		int result3 = session.delete("NewsMapper.deleteNewsById", newsid);
		session.commit();
		session.close();
		return 1;
	}
	
	

	public int newsZan(String newsId) {
		SqlSession session  =factory.openSession();
		int result = session.update("NewsMapper.newsZan", newsId);
		session.commit();
		session.close();
		return result ;
	}

	public void newsViewcountAdd(String newsId) {
		SqlSession session  =factory.openSession();
		int result = session.update("NewsMapper.newsViewcountAdd", newsId);
		session.commit();
		session.close();
		
	}

	
	public static void main(String[] args) {
		NewsDao newsdao = new NewsDaoImpl();
		newsdao.newsViewcountAdd("4df8ec5837b4446c97db05033640b6b0");
		
		
	}

}
